package com.ebt.m.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.event.LoginEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PhoneBindInstructionActivity extends BaseToolBarActivity {

    public static final String PHONE_NUMBER = "PHONE_NUMBER";

    @BindView(R.id.button)
    Button button;
    @BindView(R.id.tv_phone)
    TextView tv_phone;

    private ProgressDialog mProgressDialog;
    private String mPhoneNumber;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_bind_instruction);
        ButterKnife.bind(this);
        initProgressDialog();
        initView();
        EventBus.getDefault().register(this);
    }

    private void initProgressDialog() {
        mProgressDialog = new ProgressDialog(this, R.style.LightDialog);
        mProgressDialog.setIndeterminate(true);
        mProgressDialog.setMessage("更换手机号查询...");
    }

    private void showProgressDialog(){
        if (mProgressDialog != null && !mProgressDialog.isShowing()){
            mProgressDialog.show();
        }
    }

    private void hindProgressDialog(){
        if (mProgressDialog != null && mProgressDialog.isShowing()){
            mProgressDialog.dismiss();
        }
    }

    public void initView() {
        setTitle("手机号绑定");
        mPhoneNumber = getIntent().getStringExtra(PHONE_NUMBER);
        if (TextUtils.isEmpty(mPhoneNumber)){
            tv_phone.setText("您尚未绑定手机号");
            button.setText("绑定手机号");
        }else{
            tv_phone.setText("绑定的手机号是: " + mPhoneNumber);
            button.setText("更换手机号");
        }
    }

    @OnClick(R.id.button)
    public void onChangePhoneBindNumberClicked() {
        Intent intent = new Intent(this, BindPhoneActivity.class);
        intent.putExtra(BindPhoneActivity.MODE, BindPhoneActivity.MODE_CHANGE);
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(LoginEvent event) {
        if (event == null) {
            return;
        }
        if (!event.login){
            this.finish();
        }
    }
}
